<?php

	class stocks {
		

		function stocks() {
			global $admin,$db,$config;
		if ($_POST['act']=='export'){
				$params=$_GET;
				$sqlstr = "select a.productid,a.shop,d.pid,a.product,a.spec,a.stocks,a.shopid,b.shopwarningnum,b.canbuyout from {$config->db_prefix}stocks as a left join {$config->db_prefix}products as b on a.productid=b.product_sku_id left join haiou_product_sku as d on a.productid=d.id where 1=1 ";
				if (!empty($params['id'])) $sqlstr .= " and a.id = ".$db->qstr($params['id']);
			if (!empty($params['productid'])) $sqlstr .= " and a.productid = ".$db->qstr($params['productid']);
			if (!empty($params['shopid'])) $sqlstr .= " and a.shopid = ".$db->qstr($params['shopid']);
			if (isset($params['keywords'])) $sqlstr .= " and (productid=". $db->qstr($params['keywords']) ." or product like ". $db->qstr("%".$params['keywords']."%") .")";
			if ($params['lnumber']!==''&&$params['lnumber']!==NULL) $sqlstr .= " and stocks>=". $db->qstr($params['lnumber']);
			if ($params['hnumber']!==''&&$params['hnumber']!==NULL) $sqlstr .= " and stocks<=". $db->qstr($params['hnumber']);
			
			if($_GET['shopwarningnum']==1)$sqlstr .= " and a.stocks > b.shopwarningnum";
			if($_GET['shopwarningnum']==2)$sqlstr .= " and a.stocks <= b.shopwarningnum";
			
			if (!empty($params['cond'])) $sqlstr .= " and ({$params['cond']})";

			if (!empty($params['counter'])) $counter = $db->getrow(str_replace("*", "sum(stocks) number", $sqlstr));
			if (!empty($params['order'])) $sqlstr .= " order by {$params['order']}";
				include_once '../lib/php_excel.php';
				$export_data=$db->GetArray($sqlstr);
				$pdb = new pager(9999, 1);
				foreach ($export_data as $key=> $var)
				{
					$export_data[$key]['product']=$export_data[$key]['product']." ".$export_data[$key]['spec'];
					$repeat = true;
					$stock = $pdb->execute($db,"select id,productid, lotnumber,timeend ,(number-delnumber)as remain from {$config->db_prefix}purchases where shopid={$var['shopid']} and productid={$var['productid']} and timestocked>0 and number!=delnumber order by timeend asc,timestocked asc");
					
					//采购在途
						
					$purchases=$pdb->execute($db,"select sum(number) as purchases_nums  from {$config->db_prefix}purchases where shopid={$var['shopid']} and productid={$var['productid']} and timestocked=0 and type=0");
					
					$export_data[$key]['purchases']=(int)$purchases[0]['purchases_nums'];
						
					//预发货
					$orders=$pdb->execute($db,"select sum(number) as orders_nums  from {$config->db_prefix}orders where shopid={$var['shopid']} and productid={$var['productid']} and timestocked=0 and type=0");
					
					$export_data[$key]['orders']=(int)$orders[0]['orders_nums'];
					
					$export_data[$key]['stocks']=(int)$export_data[$key]['stocks'];
					
					$export_data[$key]['true_stocks']=(int)($export_data[$key]['stocks']-$export_data[$key]['orders']);
					
					if($export_data[$key]['true_stocks'] <= $export_data[$key]['shopwarningnum']){
						$export_data[$key]['status'] = "库存不足";
					}else{
						$export_data[$key]['status'] = "库存充足";
					}
						
					
					/* $date = date('Y-m-d',$stock[0][timeend]);
					$time = date('Y-m-d',time());
					if(strtotime($date)-strtotime($time) < 2592000 && strtotime($date)-strtotime($time) >= 0)
					{
						$dao = '快到期';
					}
					if($dao == '')
					{
						$export_data[$key]['stocks'] = '共('.count($stock).')个批次,最近保质期为:'.$date;
					}else 
					{
						$export_data[$key]['stocks'] = '共('.count($stock).')个批次,最近保质期为:'.$date.'('.$dao.')';
					} */
					unset($export_data[$key]['shopid']);
					unset($export_data[$key]['spec']);
						
					$dao = '';
				
				}
				$queue[0] = array("SKUID","仓库","产品ID","产品名称","库存总数","预警值","虚拟库存","采购在途","预发货","实际库内库存","预警状态");//定义表头
				$export_data = array_merge($queue,$export_data);
/* 				  print_r($export_data);
				 exit;  */
				$filename =   "库存信息" . date ( "YmdHis" );
				$xls = new Excel_XML ( 'UTF-8', false, '库存信息' );
				$xls->addArray ( $export_data );
				$xls->generateXML ( $filename );
				exit ();
			}
			//if(empty($_SESSION["managerid"])) $admin->login();
		}




				
		
		function _update_product_stocks($productid) {
			global $db, $segment, $config, $smarty;
			
			$stocks = $db->getone("select sum(stocks) from {$config->db_prefix}stocks where productid=$productid");
			if(!$stocks) $stocks = 0;
			$result = $db->execute("update {$config->db_prefix}products set stocks=$stocks where id=$productid");
			return $result;
		}



}
?>